import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
//删
public class JDBCDelete {
        public static void main(String[] args) throws SQLException {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入需要删除的学生姓名：");
            String a = scanner.next();
            //向上转型
            DataSource dataSource = new MysqlDataSource();
            //向下转型
            ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true");
            ((MysqlDataSource) dataSource).setUser("root");
            ((MysqlDataSource) dataSource).setPassword("666666");

            Connection connection = dataSource.getConnection();

            String sql = "delete from student where name=(?)";//?作为占位符 用于替换
            PreparedStatement statement = connection.prepareStatement(sql);
            //动态构造sql 索引从1开始.
            statement.setString(1,a);
            int n = statement.executeUpdate();
            System.out.println("n = " + n);
            //释放资源
            statement.close();
            connection.close();
        }
    }
